﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


/*程序名称：RactGame
 * 开发人：胡琦
 * 时间：20120916
 * 改进说明：
 * 20120917--难度调节需要改进，考虑增加得分机制。
 */
namespace RectGame
{
    public partial class MainForm : Form
    {
//        int mTimeInterval = 0;//时间间隔
        int mPBWidth = 0;       //绘图控件的宽
        int mPBHeight = 0;      //绘图控件的高
        int mGridHeight = 27;//网格的宽高
        int mGridWidth = 27;
        bool[,] isFill;    //记录每个格子是否被填充
        bool IsLeft = false;    //方块是否往左运动

        string hardLevel ;// 难度级别

        int clickNum; //点击次数 增加难度用，笨方法，以后要改进的地方
             

        int tempX;
        int tempY;
        int tempLenNum; //方块占几个格子

        Graphics mGraphics;
        public MainForm()
        {
            InitializeComponent();
            hardLevel = string.Empty;
            clickNum = 0;
            isFill = new bool[10, 10];
            tempX = 0;
            tempY = mGridHeight * 9;
            tempLenNum = 4;             //初始化方块的长度
            timer1.Interval =85;

            mPBWidth = pictureBox1.Width;//获取图像控件的高和宽
            mPBHeight = pictureBox1.Height;
            mGraphics = pictureBox1.CreateGraphics();
        }

        //绘制网格
        private void DrawGrid(int mPBWidth, int mPBHeight, Graphics g, int mGridHeight, int mGridWidth)
        {
            SolidBrush pBrush = new SolidBrush(Color.Green);
            Pen p = new Pen(pBrush);
            for (int i = 0; i <= mPBWidth / mGridWidth; i++)
            {
                g.DrawLine(p, i * mGridHeight, 0, i * mGridHeight, mPBHeight);
            }
            for (int j = 0; j <= mPBHeight / mGridHeight; j++)
            {
                g.DrawLine(p, 0, j * mGridWidth, mPBWidth, j * mGridWidth);
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            DrawRect(tempX, tempY, tempLenNum, e.Graphics);
            DrawFilledRect(isFill, e.Graphics);
            DrawGrid(mPBWidth, mPBHeight,e.Graphics, 27, 27);//绘制网格
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            clickNum = 0;
            isFill = new bool[10, 10];
            tempX = 0;
            tempY = mGridHeight * 9;
            tempLenNum = 4;             //初始化方块的长度
            timer1.Interval = 85;
            hardLevel = comboBox1.Text;


            pictureBox1.CreateGraphics().Clear(pictureBox1.BackColor);
            tempX = 0;
            tempY = mGridHeight * 9;
            timer1.Enabled = true;
            timer1.Start();

            //绘制最下面一行的方块 从左往右移动  长度=4
 //         DrawMoveRect(10, 4, mGraphics);

        }
        /// <summary>
        /// 填充第row行 长度为p的长方形
        /// </summary>
        /// <param name="row"></param>
        /// <param name="p"></param>
        private void DrawRect(int tempX,int tempY,int p,Graphics pGraphics)
        {
            SolidBrush mBrush=new SolidBrush(Color.Red);
            pGraphics.FillRectangle(mBrush,tempX, tempY, mGridWidth * p,mGridHeight);

        }

        private void DrawFilledRect(bool[,] isFill,Graphics pGraphics)
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (isFill[i,j])
                    {
                        DrawRect(i * mGridWidth, j * mGridHeight, 1, pGraphics);
                    }                    
                }                
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {//根据间隔移动每一行的方块
            if (!IsLeft)
            {
                tempX += mGridWidth;
                if (tempX >= mPBWidth - 1 - tempLenNum * mGridWidth) //mPBWidth = 10*mGridWidth+1   1个像素的边框
                {
                    IsLeft = true;
                }
            }
            else
            {
                tempX -= mGridWidth;
                if (tempX<=0)
                {
                    IsLeft = false;
                }
            }
            pictureBox1.Refresh();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            clickNum++;
//             int mDrawTempRectX = tempX;
//             int mDrawTempRectY = tempY;
//            DrawRect(mDrawTempRectX, mDrawTempRectY, tempLenNum, pictureBox1.CreateGraphics());
            if (tempY<0)
            {
                return;
            }
            for (int k =0; k < tempLenNum; k++)//将相应方格的isFill 赋值为 true
            {
                isFill[tempX / mGridWidth + k, tempY / mGridHeight] = true;
            }
            if (IsContinue())//如果从上到下有重叠的部分
            {
                tempY -= mGridHeight;
                if (tempY < 0)
                {
                    MessageBox.Show("恭喜你，过关了");
                    timer1.Stop();
                    return;
                }
            }
            else
            {
                timer1.Stop();
                MessageBox.Show("gg思密达，请重新开始");
            }   
            //根据行号增加难度
            if (hardLevel=="普通")
            {
                if (clickNum<=2)
                {
                    tempLenNum = 4;
                }
                else if (clickNum>2 && clickNum<=5)
                {
                    tempLenNum = 3;
                    timer1.Interval -= 10;
                }
                else if(clickNum>5 && clickNum <=7)
                {
                    tempLenNum = 2;
                    timer1.Interval -= 5;
                }
                else if (clickNum >7)
                {
                    tempLenNum = 1;
                    timer1.Interval -=5;
                }
            }
            else
            {
                tempLenNum--;
                if (tempLenNum<=0)
                {
                    tempLenNum = 1;
                }
                timer1.Interval -= 3;
            }
        }

        private bool IsContinue()
        {//若方框左上角脚点的 isfill 为ture则 方块已填充  
            bool result=false;                
            for (int i = 0; i < 10; i++)
		    {
                bool mIsContinue = true;
                for (int j = 9; j >= tempY/mGridHeight  ;j-- )
                {
                    mIsContinue = isFill[i,j] && mIsContinue;
			    }       
                if (mIsContinue)
                {
                    result = true;
                }
            }
            return result; 
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Cross;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }
    }
}
